package com.leonelviera.andronotes;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class AndroNotesWSCalls {
	
	private static final String NAMESPACE = "urn:andronotes";
	private static final String URL = "http://192.168.0.4/wss/server.php";

    private Object resultRequestSOAP = null;
    
    public Integer login(String email, String password) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "login");
     
 		request.addProperty("email", email);
 		request.addProperty("password", password);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/login", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (Integer)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return 0;
        }
    }
    
    public Integer register(String email, String password) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "register");
     
 		request.addProperty("email", email);
 		request.addProperty("password", password);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/register", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (Integer)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return 0;
        }
    }
    
    public String notasPorUsuario(int usuario_id) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "notasPorUsuario");
     
 		request.addProperty("usuario_id", usuario_id);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/notasPorUsuario", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (String)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return "";
        }
    }
    
    public Integer guardarNota(int usuario_id, String titulo, String descripcion, int modificado) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "guardarNota");
     
 		request.addProperty("usuario_id", usuario_id);
 		request.addProperty("titulo", titulo);
 		request.addProperty("descripcion", descripcion);
 		request.addProperty("modificado", modificado);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/guardarNota", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (Integer)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return 0;
        }
    }
     
    public Integer modificarNota(int nota_id, String titulo, String descripcion, int modificado) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "modificarNota");
     
 		request.addProperty("nota_id", nota_id);
 		request.addProperty("titulo", titulo);
 		request.addProperty("descripcion", descripcion);
 		request.addProperty("modificado", modificado);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/modificarNota", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (Integer)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return 0;
        }
    }
    
    public Integer borrarNota(int nota_id) {
        
    	SoapObject request = new SoapObject(NAMESPACE, "borrarNota");
     
 		request.addProperty("nota_id", nota_id);
 		
 		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        
        try {
            androidHttpTransport.call(URL + "/borrarNota", envelope);
            resultRequestSOAP =  envelope.getResponse();
            return (Integer)resultRequestSOAP;
        }
        catch (Exception e) {
        	System.out.println(e.getMessage());
        	return 0;
        }
    }
} // class
